package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.*;
import bean.*;

public class ScoreServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			String xuehao=request.getParameter("xuehao");
			String kaojuan=request.getParameter("kaojuan");
			String fenshu1=request.getParameter("fenshu1");
			int a,b;
			if(fenshu1==null||fenshu1==""){
				a=0;
			}
			else{
				a=Integer.parseInt(fenshu1);
			}
			String fenshu2=request.getParameter("fenshu2");
			if(fenshu2==null||fenshu2==""){
				b=1000;
			}
			else{
				b=Integer.parseInt(fenshu2);
			}
			String sql=null;
			if(xuehao==null||xuehao==""){
				if(kaojuan==null||kaojuan==""){
					sql="select sno,eid,score from achievement where score >= '"+a+"' and score<='"+b+"';";
				}
				else{
					sql="select sno,eid,score from achievement where eid='"+kaojuan+"' and score >= '"+a+"' and score<='"+b+"';";
				}
			}
			else{
				if(kaojuan==null||kaojuan==""){
					sql="select sno,eid,score from achievement where sno='"+xuehao+"' and score >= '"+a+"' and score<='"+b+"';";
				}
				else{
					sql="select sno,eid,score from achievement where sno='"+xuehao+"'and eid='"+kaojuan+"' and score >= '"+a+"' and score<='"+b+"';";
				}
			}
			 if(xuehao==null){
			    	xuehao="";
			    }
			 if(kaojuan==null){
			    	kaojuan="";
			    }
			 if(fenshu1==null){
				 fenshu1="";
			    }
			 if(fenshu2==null){
				 fenshu2="";
			    }
		    request.setAttribute("xuehao", xuehao);
		    request.setAttribute("kaojuan", kaojuan);
		    request.setAttribute("fenshu1", fenshu1);
		    request.setAttribute("fenshu2", fenshu2);
		    int pageSize=10;
		    int showPage;
			AchievementDao dao=new AchievementDao();
			ArrayList<Achievement> all=dao.allAchievementInSql(sql);
			int recordCount=all.size();
			int pageCount=(recordCount%pageSize==0)?(recordCount/pageSize):(recordCount/pageSize+1);
			String integer=request.getParameter("showPage");
			if(integer==null){
				   integer="1";
				  }
		    try{
		    	showPage=Integer.parseInt(integer);
		    }catch(NumberFormatException e){
		     showPage=1;
		    }
		    if(showPage<=1){
		     showPage=1;
		    }
		    if(showPage>=pageCount){
		     showPage=pageCount;
		    }
		   
		    if(showPage!=pageCount){
		    	int position=(showPage-1)*pageSize+1;
		    	ArrayList<Achievement> al=dao.getOnePage(position, pageSize,sql);
		    	request.setAttribute("alist", al);
		    }
		    else{
		    	int position=(showPage-1)*pageSize+1;
		    	ArrayList<Achievement> al=dao.getLastPage(position,sql);
		    	request.setAttribute("alist", al);
		    }
		    request.setAttribute("pageCount", pageCount);
		    request.setAttribute("showPage", showPage);
		   
			request.getRequestDispatcher("scoremanage.jsp").forward(request,response);
		}
	      

}
